package com.wyq.new_springboot.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wyq.new_springboot.config.redis.RedisService;
import com.wyq.new_springboot.entity.TBlog;
import com.wyq.new_springboot.entity.TBlogExtend;
import com.wyq.new_springboot.service.ITBlogService;
import com.wyq.new_springboot.util.result.Result;
import com.wyq.new_springboot.util.result.ResultGenerator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.net.util.IPAddressUtil;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章管理
 * @Author weiyaqiang
 * @create 2021/3/26 11:27
 */
@Controller
@RequestMapping("/articleManage")
public class ArticleManageController {

    @Autowired
    ITBlogService itBlogService;

    @Autowired
    RedisService redisService;
    /**
     * 跳转到文章管理页面
     * @return
     */
    @RequestMapping("/index")
    public String toPage(Model model){
        return "articleManage";
    }
    @RequestMapping("/getPage")
    @ResponseBody
    public Map getPage(@RequestParam String page, @RequestParam String limit){
        //先从redis去取值，如果没有的话，从数据库中取值，然后放在redis中
        String blogs = (String)redisService.get("blogs");
        List<TBlog> allBlog=new ArrayList<>();
        if (blogs != null) {
            //将json字符串装成json数组
            allBlog = JSONUtil.toList(blogs, TBlog.class);
        } else {
            //存放所有的记录数
            allBlog = itBlogService.getAllBlog();
            String s = JSONUtil.toJsonStr(allBlog);
            redisService.set("blogs",s);
            redisService.expire("blogs",60);
        }
        Map<String,Object> map=new HashMap<>();
        List<TBlog> pages = itBlogService.getPages(Long.valueOf(page), Long.valueOf(limit));
        List<TBlogExtend> pagess=new ArrayList<>();
        pages.forEach(i->{
            LocalDateTime createTime = i.getCreateTime();
            long l = createTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
            TBlogExtend tBlogExtend= new TBlogExtend();
            BeanUtil.copyProperties(i, tBlogExtend);//对象属性拷贝
            tBlogExtend.setTimestamp(l);
            pagess.add(tBlogExtend);
        });
        map.put("code",0);
        map.put("msg","");
        map.put("count",allBlog.size());
        map.put("data",pagess);
        return map;
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable int id,Model model){
        TBlog byId = itBlogService.getById(id);
        model.addAttribute("blog",byId);
        return "forward:/article/article";
    }

//    @RequiresRoles("admin")
    @RequiresPermissions("blog:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestParam int id){
        itBlogService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }
}
